#*****
# 百度点击
#*****

import concurrent.futures
import time
import asyncio
import random

from playwright.async_api import async_playwright


B = [1,2,3,4,5,7,8,9,11,22,33,44,55,66,77,88,99]
num = 3
length = len(B)

async def on_response(response):

   if 'baidu.com/mcp/pc/pcsearch' in response.url:
       resp = await response.text()

       print(resp)





async def gorun(context, nn):
    page = await context.new_page()
    page.on('response', on_response)
    await page.goto("https://www.baidu.com/")
    await page.locator("#kw").click()
    await page.locator("#kw").fill(nn)
    await page.locator("#kw").press("Enter")
    await asyncio.sleep(random.randrange(3,10))

    await page.close()

async def main() -> None:
  async with async_playwright() as playwright:
    browser = await playwright.chromium.launch(headless=False,
                                               slow_mo=2000
                                               )
    context = await browser.new_context()

    tasks = []
    nnn = 111
    task = asyncio.create_task(gorun(context, str(nnn)))  ##协程对象
    tasks.append(task)

    for t in tasks:
        await t



async def main2() -> None:
  async with async_playwright() as playwright:
    browser = await playwright.chromium.launch(headless=False,
                                               slow_mo=2000
                                               )
    context = await browser.new_context()

    for i in range(0, length, num):
      tasks = []
      for nn in B[i:i + num]:
        task = asyncio.create_task(gorun(context, str(nn)))  ##协程对象
        tasks.append(task)

      for t in tasks:
        await t


asyncio.run(main())